﻿Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Public Class MyDataGridViewPage

    Private bRefresh As Boolean = False    '保存状态是否刷新
    Private bLoadData As Boolean = True    '是否在加载数据
    Public rs As ADODB.Recordset           '临时对象
    'A-2009-08-30
    Public bCmd As Boolean = False
    Public cmdSql As New ADODB.Command
    Public cmdSqlCount As New ADODB.Command
    Private ct As Wys.Db.Vb.CTable
    'A-2009-08-30
#Region "设置窗体自动大小 控件也自动调整大小"
    'Public X As Single '初始化窗体的宽度
    'Public Y As Single '初始化窗体的高度  
    ''' <summary>
    ''' 在继承窗体Load 事件中的最后一行调用
    ''' </summary>
    ''' <remarks></remarks>
    Public Overridable Sub Init()
        'X = Me.Width
        'Y = Me.Height
        'setTag(Me)        '     
        '根据缩放的高度(自动调整每页的记录个数)
        'If UTIL.gloabs.ScaleHeight <> CType(1, Single) Then
        '    PageSize = Math.Round(PageSize * UTIL.gloabs.ScaleHeight)
        'End If
    End Sub
#End Region

#Region "重载Resize事件"
    '''' 
    '''' <summary>
    '''' 这个写的不好，应该单写一个，在父窗体调
    '''' </summary>
    '''' <param name="sender"></param>
    '''' <param name="e"></param>
    '''' <remarks></remarks>
    'Public Sub AutoAdjustFrom_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    'Dim newx As Single = (Me.Width) / X
    'Dim newy As Single = Me.Height / Y
    '    setControls(newx, newy, Me)
    'End Sub

#End Region

#Region "窗体初始化事件"

    Private Sub MyDataGridViewPage_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        Try
            Me.MyDataView1.Rows.Clear()
            Me.MyDataView1.Dispose()
            If rs IsNot Nothing Then
                If rs.State = ADODB.ObjectStateEnum.adStateOpen Then
                    rs.Close()
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(rs) '20100531
                End If
                rs = Nothing
            End If
            cmdSql = Nothing
            cmdSqlCount = Nothing
            GC.Collect()
        Catch ex As Exception

        End Try
    End Sub
    ''' <summary>
    ''' 窗体初始化事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub MyDataGridViewPage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.MyDataView1.SetInfo()
        Init()
        ct = ResouceSpool.ct
        lblCurrentPage.TextAlign = ContentAlignment.MiddleCenter
        lblTotal.TextAlign = ContentAlignment.MiddleCenter
    End Sub
#End Region

#Region "窗体大小改变"
    ''' <summary>
    ''' 窗体大小改变
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub MyDataGridViewPage_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        'Dim newx As Single = (Me.Width) / X
        'Dim newy As Single = Me.Height / Y
        'setControls(newx, newy, Me)
        Me.MyDataView1.Top = lblTotal.Top + lblTotal.Height + 3
        Me.MyDataView1.Height = btnPre.Top - 3 - Me.MyDataView1.Top
    End Sub
#End Region

#Region "常用的方法"
    'A-2009-08-30
    Public Sub SetRsSQLITE(ByVal strSqlCmd As ADODB.Command, ByVal strCountCmd As ADODB.Command, ByVal ct As Wys.Db.Vb.CTable, ByVal page As Integer)
        _mode = 0
        bCmd = True
        '如果哦是两个SQL相同那就是删除或更新调用
        Dim bReLoad As Boolean = False
        If (strSqlCmd.CommandText = cmdSql.CommandText) And (cmdSqlCount.CommandText = strCountCmd.CommandText) Then
            bReLoad = True
        End If
        cmdSql = strSqlCmd
        strCountCmd = strCountCmd
        rs = ct.ExcCommandAsRs(strCountCmd)
        If rs IsNot Nothing Then
            If rs.EOF = False Then
                If (String.IsNullOrEmpty(rs.Fields("PageCount").Value.ToString())) Then
                    TotalCount = 0
                Else
                    TotalCount = Convert.ToInt32(rs.Fields("PageCount").Value)
                End If

                Dim PageCount1 As Double = TotalCount / PageSize
                Dim pageCount2 As Double = CType(TotalCount Mod PageSize, Double) / PageSize
                PageCount = System.Convert.ToInt32(PageCount1 - pageCount2)
                If TotalCount <= PageSize Then
                    PageCount = 1
                Else
                    If (TotalCount Mod PageSize > 0) Then
                        PageCount = PageCount + 1
                    End If
                End If
            End If
            rs.Close()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs) '20100531
            rs = Nothing
        End If

        Dim iTempPageIndex = PageIndex
        If PageCount > 0 Then
            PageIndex = page
            If page <= 0 Then
                PageIndex = 1
            End If
            If page >= PageCount Then
                PageIndex = PageCount
            End If
        End If

        If bReLoad = True Then
            PageIndex = iTempPageIndex
            If PageIndex <= 0 Then
                PageIndex = 1
            End If
            If PageIndex >= PageCount Then
                PageIndex = PageCount
            End If
        End If

        Dim strFilter As String = "Limit " + PageSize.ToString() + "  Offset " + ((PageIndex - 1) * PageSize).ToString()
        strSqlCmd.CommandText = strSqlCmd.CommandText + " " + strFilter
        rs = ct.ExcCommandAsRs(strSqlCmd)
        If Me.rs IsNot Nothing Then
            If rs.EOF = True Then
                PageIndex = 0  '当前页为零
                PageCount = 0  '总页数为零
                TotalCount = 0 '总记录数为零
            End If
        End If

        Me.MyDataView1.Rows.Clear()
        bLoadData = True
        If rs IsNot Nothing Then
            If rs.EOF = False Then
                Dim obs(2) As Object
                Dim iCount As Integer = 0
                While Not (rs.EOF)
                    obs(0) = rs.Fields(ColName1).Value
                    obs(1) = rs.Fields(ColName2).Value
                    Me.MyDataView1.Rows.Add(obs)
                    iCount = iCount + 1
                    If iCount >= Me.PageSize Then
                        Exit While
                    End If
                    rs.MoveNext()
                End While
            End If
        End If
        If rs.State = ADODB.ObjectStateEnum.adStateOpen Then
            rs.Close()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs) '20100531
        End If
        rs = Nothing
        bLoadData = False
        '设置下面的按钮
        RefreshButton()
        '设置当前选的位置
        SetSelectPos(PagePos)
        '
        If MyDataView1.Rows.Count = 0 Then
            RaiseEvent ItemClickRefreshData("", bState)
        End If

        RaiseEvent EventPageComplete() 'A-2010-02-14
    End Sub
    'A-2009-08-30

    Public Sub SetRsSQLITE(ByVal strSql As String, ByVal strCount As String, ByVal ct As Wys.Db.Vb.CTable, ByVal page As Integer)
        _mode = 0
        '如果哦是两个SQL相同那就是删除或更新调用
        Dim bReLoad As Boolean = False
        If (strSql = Sql) And (StrSqlLiteSql1 = strCount) Then
            bReLoad = True
        End If
        Sql = strSql
        StrSqlLiteSql1 = strCount
        'A-2009-10-11
        If Not (BrowserData) Then
            strCount = UTIL.CUtil.GetSqlWhere(strCount, UserName + "='" + UserNameValue + "'")
        End If
        'A-2009-10-11
        rs = ct.GetTable(strCount)

        If rs IsNot Nothing Then
            If rs.EOF = False Then
                If (String.IsNullOrEmpty(rs.Fields("PageCount").Value.ToString())) Then
                    TotalCount = 0
                Else
                    TotalCount = Convert.ToInt32(rs.Fields("PageCount").Value)
                End If

                Dim PageCount1 As Double = TotalCount / PageSize
                Dim pageCount2 As Double = CType(TotalCount Mod PageSize, Double) / PageSize
                PageCount = System.Convert.ToInt32(PageCount1 - pageCount2)
                If TotalCount <= PageSize Then
                    PageCount = 1
                Else
                    If (TotalCount Mod PageSize > 0) Then
                        PageCount = PageCount + 1
                    End If
                End If
            End If
            rs.Close()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs) '20100531
            rs = Nothing
        End If

        Dim iTempPageIndex = PageIndex
        If PageCount > 0 Then
            PageIndex = page
            If page <= 0 Then
                PageIndex = 1
            End If
            If page >= PageCount Then
                PageIndex = PageCount
            End If
        End If

        If bReLoad = True Then
            PageIndex = iTempPageIndex
            If PageIndex <= 0 Then
                PageIndex = 1
            End If
            If PageIndex >= PageCount Then
                PageIndex = PageCount
            End If
        End If

        Dim strFilter As String = "Limit " + PageSize.ToString() + "  Offset " + ((PageIndex - 1) * PageSize).ToString()
        'A-2009-10-11
        If Not (BrowserData) Then
            strSql = UTIL.CUtil.GetSqlWhere(strSql, UserName + "='" + UserNameValue + "'")
        End If
        'A-2009-10-11
        strSql = strSql + " " + strFilter
        rs = ct.GetTable(strSql)
        If Me.rs IsNot Nothing Then
            If rs.EOF = True Then
                PageIndex = 0  '当前页为零
                PageCount = 0  '总页数为零
                TotalCount = 0 '总记录数为零
            End If
        End If

        Me.MyDataView1.Rows.Clear()
        bLoadData = True
        If rs IsNot Nothing Then
            If rs.EOF = False Then
                Dim obs(2) As Object
                Dim iCount As Integer = 0
                While Not (rs.EOF)
                    obs(0) = rs.Fields(ColName1).Value
                    obs(1) = rs.Fields(ColName2).Value
                    Me.MyDataView1.Rows.Add(obs)
                    iCount = iCount + 1
                    If iCount >= Me.PageSize Then
                        Exit While
                    End If
                    rs.MoveNext()
                End While
            End If
        End If

        If rs IsNot Nothing Then
            If rs.State = ADODB.ObjectStateEnum.adStateOpen Then
                rs.Close()
                System.Runtime.InteropServices.Marshal.ReleaseComObject(rs) '20100531
            End If
        End If

        rs = Nothing
        bLoadData = False
        '设置下面的按钮
        RefreshButton()
        '设置当前选的位置
        SetSelectPos(PagePos)
        '
        If MyDataView1.Rows.Count = 0 Then
            RaiseEvent ItemClickRefreshData("", bState)
        End If
        Me.txtPage.Text = PageIndex.ToString()

        RaiseEvent EventPageComplete() 'A-2010-02-14
    End Sub
    ''' <summary>
    ''' 刷新下面四个按钮状态
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub RefreshButton()
        '没有数据时
        If PageCount = 0 Then
            Me.btnFirst.Enabled = False
            Me.btnPre.Enabled = False
            Me.txtPage.Enabled = False
            Me.btnNext.Enabled = False
            Me.btnLast.Enabled = False
            RefrshLabel()
            Return
        End If
        '就一页数据时
        If PageCount = 1 Then
            Me.btnFirst.Enabled = False
            Me.btnPre.Enabled = False
            Me.txtPage.Enabled = False
            Me.btnNext.Enabled = False
            Me.btnLast.Enabled = False
            RefrshLabel()
            Return
        End If
        '如果为第一页
        If PageIndex <= 1 Then
            Me.btnFirst.Enabled = False
            Me.btnPre.Enabled = False
            Me.txtPage.Enabled = True
            Me.btnNext.Enabled = True
            Me.btnLast.Enabled = True
            RefrshLabel()
            Return
        End If
        '如果为最后一页
        If PageIndex >= PageCount Then
            Me.btnFirst.Enabled = True
            Me.btnPre.Enabled = True
            Me.txtPage.Enabled = True
            Me.btnNext.Enabled = False
            Me.btnLast.Enabled = False
            RefrshLabel()
            Return
        End If
        '否则得话
        Me.btnFirst.Enabled = True
        Me.btnPre.Enabled = True
        Me.txtPage.Enabled = True
        Me.btnNext.Enabled = True
        Me.btnLast.Enabled = True
        RefrshLabel()
        Return
    End Sub
    ''' <summary>
    ''' 刷新标签
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub RefrshLabel()
        'If rs Is Nothing Then
        '    Me.lblCurrentPage.Text = ""
        '    Me.lblTotal.Text = ""
        '    Return
        'End If
        'If rs.EOF = True Then
        '    Me.lblCurrentPage.Text = ""
        '    Me.lblTotal.Text = ""
        '    Return
        'End If
        Me.lblCurrentPage.Text = "当前页:" + PageIndex.ToString() + "/" + PageCount.ToString()
        Me.lblTotal.Text = "共有" + TotalCount.ToString() + "条记录"
    End Sub
#End Region

#Region "设置常用属性"
    Private _TotalCount As Integer = 0
    ''' <summary>
    ''' 得到数据库记录数
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Property TotalCount() As Integer
        Get
            Return _TotalCount
        End Get
        Set(ByVal value As Integer)
            _TotalCount = value
        End Set
    End Property
    ''' <summary>
    ''' 设置连接的SQL 
    ''' </summary>
    ''' <remarks></remarks>
    Private _Sql As String = String.Empty
    Public Property Sql() As String
        Get
            Return _Sql
        End Get
        Set(ByVal value As String)
            _Sql = value
        End Set
    End Property
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private _strSqlLiteSql1 As String
    Public Property StrSqlLiteSql1() As String
        Get
            Return _strSqlLiteSql1
        End Get
        Set(ByVal value As String)
            _strSqlLiteSql1 = value
        End Set
    End Property


    ''' <summary>
    ''' 设置每页显示多少条记录
    ''' </summary>
    ''' <remarks></remarks>
    Private _PageSize As Integer = 20
    Public Property PageSize() As Integer
        Get
            Return _PageSize
        End Get
        Set(ByVal value As Integer)
            _PageSize = value
        End Set
    End Property

    ''' <summary>
    ''' 设置当前的页数
    ''' </summary>
    ''' <remarks></remarks>
    Private _PageIndex As Integer = 1
    Public Property PageIndex() As Integer
        Get
            Return _PageIndex
        End Get
        Set(ByVal value As Integer)
            _PageIndex = value
        End Set
    End Property
    ''' <summary>
    ''' 设置总页数
    ''' </summary>
    ''' <remarks></remarks>
    Private _PageCount As Integer = 0
    Public Property PageCount() As Integer
        Get
            Return _PageCount
        End Get
        Set(ByVal value As Integer)
            _PageCount = value
        End Set
    End Property
    ''' <summary>
    ''' 设置字段一
    ''' </summary>
    ''' <remarks></remarks>
    Private _ColName1 As String = "ID"
    Public Property ColName1() As String
        Get
            Return _ColName1
        End Get
        Set(ByVal value As String)
            _ColName1 = value
        End Set
    End Property
    ''' <summary>
    ''' 设置字段二 
    ''' </summary>
    ''' <remarks></remarks>
    Private _ColName2 As String = "TITLE"
    Public Property ColName2() As String
        Get
            Return _ColName2
        End Get
        Set(ByVal value As String)
            _ColName2 = value
        End Set
    End Property
    ''' <summary>
    ''' 设置字段二 
    ''' </summary>
    ''' <remarks></remarks>
    Private _ColName2Text As String = "标题"
    Public Property ColName2Text() As String
        Get
            Return _ColName2Text
        End Get
        Set(ByVal value As String)
            If Me.MyDataView1.Column2 IsNot Nothing Then
                _ColName2Text = value
                Me.MyDataView1.Column2.HeaderText = _ColName2Text
            End If
        End Set
    End Property

    ''' <summary>
    ''' 当前页的位置
    ''' </summary>
    ''' <remarks></remarks>
    Private _PagePos As Integer = 0
    Private Property PagePos() As Integer
        Get
            Return _PagePos
        End Get
        Set(ByVal value As Integer)
            _PagePos = value
        End Set
    End Property
#End Region

#Region "封装的按钮事件"
    ''' <summary>
    ''' 第一页按钮
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        PageIndex = 1
        PagePos = 0
        'RefreshData()
        'A-2009-08-30
        If _mode = 0 Then
            If bCmd Then
                SetRsSQLITE(Me.cmdSql, Me.cmdSqlCount, ct, PageIndex)
            Else
                SetRsSQLITE(Sql, StrSqlLiteSql1, ct, PageIndex)
            End If
        Else
            LoadPageDataTable(PageIndex)
        End If
       
        Me.txtPage.Text = PageIndex.ToString()
        'A-2009-08-30
        'D-2009-08-30
        'SetRsSQLITE(Sql, StrSqlLiteSql1, ct, PageIndex)
        'D-2009-08-30
        RaiseEvent EventPageComplete() 'A-2009-08-29

    End Sub
    ''' <summary>
    ''' 上一页按钮
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnPre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPre.Click
        If btnPre.Enabled = False Then
            Exit Sub
        End If
        PageIndex = PageIndex - 1
        PagePos = 0
        'RefreshData()
        'A-2009-08-30
        If _mode = 0 Then
            If bCmd Then
                SetRsSQLITE(Me.cmdSql, Me.cmdSqlCount, ct, PageIndex)
            Else
                SetRsSQLITE(Sql, StrSqlLiteSql1, ct, PageIndex)
            End If
        Else
            LoadPageDataTable(PageIndex)
        End If
      
        Me.txtPage.Text = PageIndex.ToString()
        'A-2009-08-30
        'D-2009-08-30
        'SetRsSQLITE(Sql, StrSqlLiteSql1, ct, PageIndex)
        RaiseEvent EventPageComplete() 'A-2009-08-29
    End Sub
    ''' <summary>
    ''' 下一页按钮
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If btnNext.Enabled = False Then
            Exit Sub
        End If
        '设置页数刷新数据
        PageIndex = PageIndex + 1
        PagePos = 0
        'RefreshData()
        'A-2009-08-30
        If _mode = 0 Then
            If bCmd Then
                SetRsSQLITE(Me.cmdSql, Me.cmdSqlCount, ct, PageIndex)
            Else
                SetRsSQLITE(Sql, StrSqlLiteSql1, ct, PageIndex)
            End If
        Else
            LoadPageDataTable(PageIndex)
        End If
        
        Me.txtPage.Text = PageIndex.ToString()
        'A-2009-08-30
        'D-2009-08-30
        'SetRsSQLITE(Sql, StrSqlLiteSql1, ct, PageIndex)
        RaiseEvent EventPageComplete() 'A-2009-08-29

    End Sub
    ''' <summary>
    ''' 最后一页按钮
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        '设置页数刷新数据
        PageIndex = PageCount
        PagePos = 0
        'RefreshData()
        'A-2009-08-30
        If _mode = 0 Then
            If bCmd Then
                SetRsSQLITE(Me.cmdSql, Me.cmdSqlCount, ct, PageIndex)
            Else
                SetRsSQLITE(Sql, StrSqlLiteSql1, ct, PageIndex)
            End If
        Else
            LoadPageDataTable(PageIndex)
        End If
        Me.txtPage.Text = PageIndex.ToString()
        'A-2009-08-30
        'D-2009-08-30
        'SetRsSQLITE(Sql, StrSqlLiteSql1, ct, PageIndex)
        RaiseEvent EventPageComplete() 'A-2009-08-29

    End Sub
    ''' <summary>
    ''' 查询页
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtPage_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPage.KeyDown
        If e.KeyCode = Keys.Return Then
            Try
                If String.IsNullOrEmpty(Me.txtPage.Text) Then
                    Return
                End If
                PageIndex = Integer.Parse(Me.txtPage.Text)
                'RefreshData()
                'A-2009-08-30
                If _mode = 0 Then
                    If bCmd Then
                        SetRsSQLITE(Me.cmdSql, Me.cmdSqlCount, ct, PageIndex)
                    Else
                        SetRsSQLITE(Sql, StrSqlLiteSql1, ct, PageIndex)
                    End If
                Else
                    LoadPageDataTable(PageIndex)
                End If
                'A-2009-08-30
                'D-2009-08-30
                'SetRsSQLITE(Sql, StrSqlLiteSql1, ct, PageIndex)
                RaiseEvent EventPageComplete() 'A-2009-08-29
            Catch ex As Exception

            End Try
        End If
    End Sub

#End Region

#Region "单击时触发事件"
    '自定义事件
    Public bState As Boolean = False '判断事件是否执行 
    Public Event ItemClickRefreshData(ByVal strId As String, ByRef bState As Boolean)
    Private Sub MyDataView_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyDataView1.Resize
        If MyDataView1.ColumnCount > 0 Then
            If MyDataView1.Width > 7 AndAlso MyDataView1.ColumnCount = 2 Then
                MyDataView1.Column2.Width = Me.Width - 2
            End If
        End If
    End Sub

    ''' <summary>
    ''' 鼠标单击事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub MyDataView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyDataView1.Click
        If bState Then
            Threading.Thread.Sleep(50)
            Return
        End If
        If Me.MyDataView1.SelectedRows.Count > 0 Then
            '避免冲掉数据 默认选择第一条
            If MyDataView1.SelectedRows(0).Index = 0 Then
            Else
                PagePos = MyDataView1.SelectedRows(0).Index
            End If
            'PagePos = MyDataView1.SelectedRows(0).Index
            RaiseEvent ItemClickRefreshData(MyDataView1.SelectedRows(0).Cells(0).Value, bState)
            'If MyDataView1.Rows.Count = 0 Then
            '    RaiseEvent ItemClickRefreshData("-1")
            'End If
        End If
    End Sub

    ''' <summary>
    ''' 选择行改变
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub MyDataView_RowStateChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowStateChangedEventArgs) Handles MyDataView1.RowStateChanged
        'If bState Then
        '    Threading.Thread.Sleep(50)
        '    Return
        'End If
        'If bLoadData = False Then
        '    If MyDataView1.SelectedRows.Count > 0 Then
        '        If MyDataView1.SelectedRows(0).Cells.Count > 0 Then
        '            '避免冲掉数据
        '            If MyDataView1.SelectedRows(0).Index = 0 Then
        '            Else
        '                PagePos = MyDataView1.SelectedRows(0).Index
        '            End If
        '            RaiseEvent ItemClickRefreshData(MyDataView1.SelectedRows(0).Cells(0).Value, bState)
        '        End If
        '        'If MyDataView1.Rows.Count = 0 Then
        '        '    RaiseEvent ItemClickRefreshData("-1")
        '        'End If
        '    End If
        'End If
    End Sub
    '''' <summary>
    '''' 好像没有用的函数
    '''' </summary>
    '''' <remarks></remarks>
    'Public Sub SetSelectPos()
    '    If PagePos < 0 Then Exit Sub
    '    If PagePos >= MyDataView1.Rows.Count Then Exit Sub
    '    MyDataView1.Rows(PagePos).Selected = True
    'End Sub
    ''' <summary>
    ''' 设置选中记录
    ''' </summary>
    ''' <param name="strpos"></param>
    ''' <remarks></remarks>
    Public Sub SetSelectPos(ByVal strpos As Integer)
        Try
            If bState Then
                Threading.Thread.Sleep(50)
                Return
            End If
            If MyDataView1.Rows.Count = 0 Then Exit Sub
            If strpos < 0 Then strpos = 0
            If strpos >= MyDataView1.Rows.Count - 1 Then strpos = MyDataView1.Rows.Count - 1
            MyDataView1.Rows(strpos).Selected = True
            MyDataView1.FirstDisplayedScrollingRowIndex = strpos
            '发行事件，选中当前行
            RaiseEvent ItemClickRefreshData(MyDataView1.SelectedRows(0).Cells(0).Value, bState)
        Catch ex As Exception
        Finally
        End Try

    End Sub

    Public Sub SetNextSelectPos(ByVal strpos As Integer)
        Try
            If bState Then
                Threading.Thread.Sleep(50)
                Return
            End If
            If MyDataView1.Rows.Count = 0 Then Exit Sub
            If strpos < 0 Then strpos = 0
            If strpos >= MyDataView1.Rows.Count - 1 Then strpos = MyDataView1.Rows.Count - 1
            MyDataView1.Rows(strpos).Selected = True
            MyDataView1.FirstDisplayedScrollingRowIndex = strpos
            '发行事件，选中当前行
            RaiseEvent ItemClickRefreshData(MyDataView1.SelectedRows(0).Cells(0).Value, bState)
        Catch ex As Exception
        Finally
        End Try

    End Sub

    Public Sub SetPreSelectPos(ByVal strpos As Integer)
        Try
            If bState Then
                Threading.Thread.Sleep(50)
                Return
            End If
            If MyDataView1.Rows.Count = 0 Then Exit Sub
            If strpos < 0 Then strpos = 0
            If strpos >= MyDataView1.Rows.Count - 1 Then strpos = MyDataView1.Rows.Count - 1
            MyDataView1.Rows(strpos).Selected = True
            MyDataView1.FirstDisplayedScrollingRowIndex = strpos
            '发行事件，选中当前行
            RaiseEvent ItemClickRefreshData(MyDataView1.SelectedRows(0).Cells(0).Value, bState)
        Catch ex As Exception
        Finally
        End Try

    End Sub
#End Region

#Region "返回当前显示的所有行"
    Public Function GetAllRow() As System.Windows.Forms.DataGridViewRowCollection
        Return Me.MyDataView1.Rows
    End Function
#End Region

#Region "重写键盘事件"
    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Left Then
            Me.btnPre_Click(Nothing, Nothing)
        End If

        If keyData = Keys.Right Then
            Me.btnNext_Click(Nothing, Nothing)
        End If

        If keyData = Keys.Home Then
            Me.btnFirst_Click(Nothing, Nothing)
        End If

        If keyData = Keys.End Then
            Me.btnLast_Click(Nothing, Nothing)
        End If

        If keyData = Keys.Up Then
            If bState Then
                Threading.Thread.Sleep(50)
                Return False
            Else
                SetPreSelectPos(Me.当前的选择行的索引() - 1)
                'Return MyBase.ProcessCmdKey(msg, keyData)
                Return False
            End If
        End If

        If keyData = Keys.Down Then
            If bState Then
                Threading.Thread.Sleep(50)
                Return False
            Else
                SetNextSelectPos(Me.当前的选择行的索引() + 1)
                'Return MyBase.ProcessCmdKey(msg, keyData)
                Return False
            End If
        End If

        If keyData = Keys.Tab Or keyData = Keys.F6 Then
            Dim frm As Form = Me.Parent
            If frm IsNot Nothing Then
                frm.SelectNextControl(Me, True, True, False, True)
                Return True
            End If
        ElseIf keyData = Keys.F7 Then
            Dim frm As Form = Me.Parent
            If frm IsNot Nothing Then
                frm.SelectNextControl(Me, False, True, True, True)
                Return True
            End If
        End If

        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
#End Region

#Region "新增翻页后事件 A-2009-08-29"
    Public Event EventPageComplete()
#End Region

#Region "设置模式 A-2009-09-15 D-2009-09-25"
    'Public Function SetDefaultFocus() As Boolean
    '    SetDefaultFocus = False
    '    Me.MyDataView1.Focus()
    '    If Me.MyDataView1.Focused Then
    '        SetDefaultFocus = True
    '    End If
    'End Function
#End Region

#Region "判断当前选择是不是最后一条记录 A-2009-09-15"
    Public Function GetLastRecordFromPage() As Boolean
        GetLastRecordFromPage = False
        If Me.MyDataView1.Rows.Count = 0 Then
            Exit Function
        End If

        If Me.MyDataView1.SelectedRows.Count = 0 Then
            Exit Function
        End If

        If Me.MyDataView1.SelectedRows(0).Index = Me.MyDataView1.Rows(Me.MyDataView1.Rows.Count - 1).Index Then
            GetLastRecordFromPage = True
        End If

    End Function
#End Region

#Region "判断当前选择是不是第一条记录 A-2009-09-25"
    Public Function GetFirstRecordFromPage() As Boolean
        GetFirstRecordFromPage = False
        If Me.MyDataView1.Rows.Count = 0 Then
            Exit Function
        End If

        If Me.MyDataView1.SelectedRows.Count = 0 Then
            Exit Function
        End If

        If Me.MyDataView1.SelectedRows(0).Index = 0 Then
            GetFirstRecordFromPage = True
        End If

    End Function
#End Region

#Region "返回当前显示的实际行数 A-2010-05-01"
    Public Function RowCount() As Integer
        Return Me.MyDataView1.Rows.Count
    End Function
#End Region

#Region "自动滚动记录 A-2009-09-25"
    Public Sub ScrollRecord(ByVal iIndex As Integer)

        If MyDataView1.Rows.Count = 0 Then
            Exit Sub
        End If

        Me.SetSelectPos(iIndex)

    End Sub
#End Region

#Region "翻页 到固定页 A-2010-05-01"
    '翻到固定位置
    Public Sub ScrollPagePosition(ByVal iDirect As Integer)
        '向后翻页
        Me.txtPage.Text = iDirect.ToString()
        If String.IsNullOrEmpty(Me.txtPage.Text) Then
            Return
        End If
        PageIndex = Integer.Parse(Me.txtPage.Text)
        If _mode <> 1 Then
            If bCmd Then
                SetRsSQLITE(Me.cmdSql, Me.cmdSqlCount, ct, PageIndex)
            Else
                SetRsSQLITE(Sql, StrSqlLiteSql1, ct, PageIndex)
            End If
        Else
            LoadPageDataTable(PageIndex)
        End If
        RaiseEvent EventPageComplete()
    End Sub
#End Region

#Region "设置是否可以浏览其他用户数据 A-2009-10-11"
    Private _browserData As Boolean = False
    Public Property BrowserData() As Boolean
        Get
            Return _browserData
        End Get
        Set(ByVal value As Boolean)
            _browserData = value
        End Set
    End Property
#End Region

#Region "设置用户的值 A-2009-10-11"
    Private _UserNameValue As String = ""
    Public Property UserNameValue() As String
        Get
            Return _UserNameValue
        End Get
        Set(ByVal value As String)
            _UserNameValue = value
        End Set
    End Property
#End Region

#Region "设置用户的字段名 A-2009-10-11"
    Private _UserName As String = ""
    Public Property UserName() As String
        Get
            Return _UserName
        End Get
        Set(ByVal value As String)
            _UserName = value
        End Set
    End Property
#End Region

#Region "得到当前页 A-2009-11-09"
    Public Function GetCurrentPage() As Integer
        Return PageIndex
    End Function
#End Region

#Region "双击事件 A-2010-02-17"
    Public Event ItemDoubleClickRefreshData(ByVal strId As String, ByRef bState As Boolean) 'A-2010-02-17
    Private Sub MyDataView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyDataView1.DoubleClick
        If bState Then
            Threading.Thread.Sleep(50)
            Return
        End If
        If Me.MyDataView1.SelectedRows.Count > 0 Then
            '避免冲掉数据 默认选择第一条
            If MyDataView1.SelectedRows(0).Index = 0 Then
            Else
                PagePos = MyDataView1.SelectedRows(0).Index
            End If
            RaiseEvent ItemDoubleClickRefreshData(MyDataView1.SelectedRows(0).Cells(0).Value, bState)
        End If
    End Sub
#End Region
    
#Region "只能入力数字 A-2010-03-08"
    Private Sub txtPage_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPage.KeyPress
        '应许输入退格键 C# 强制转换
        If Asc(e.KeyChar) <> 8 Then
            '正则验证数字
            If (Not Regex.IsMatch(e.KeyChar.ToString(), "^[0-9]$")) Then
                e.Handled = True
                Return
            End If
        End If
    End Sub
#End Region

#Region "设置数据库连接"

#End Region

#Region "当前的选择行的索引 A-2010-05-26"
    Public Function 当前的选择行的索引() As Integer
        If Me.MyDataView1.RowCount = 0 Then
            Return -1
        End If
        If Me.MyDataView1.SelectedRows.Count = 0 Then
            Return -1
        End If
        Return MyDataView1.SelectedRows(0).Index
    End Function
#End Region

#Region "添加支持DATATABLE的数据源 A-2010-06-21"
    Dim _dt As DataTable
    Dim _mode As Integer = 0
    Public Sub SetDataSource(ByVal dt As DataTable, ByVal Mode As Integer)
        _dt = dt
        _mode = Mode

        _Sql = ""
        _strSqlLiteSql1 = ""

        If _dt.Rows.Count = 0 Then
            TotalCount = 0
        Else
            TotalCount = _dt.Rows.Count
        End If

        Dim PageCount1 As Double = TotalCount / PageSize
        Dim pageCount2 As Double = CType(TotalCount Mod PageSize, Double) / PageSize
        PageCount = System.Convert.ToInt32(PageCount1 - pageCount2)
        If TotalCount <= PageSize Then
            PageCount = 1
        Else
            If (TotalCount Mod PageSize > 0) Then
                PageCount = PageCount + 1
            End If
        End If

        LoadPageDataTable(0)
    End Sub

    Public Sub LoadPageDataTable(ByVal PageIndex As Integer)
        Me.MyDataView1.Rows.Clear()
        _Sql = ""
        _strSqlLiteSql1 = ""

        Dim SkinCount As Integer = (PageIndex - 1) * PageSize
        If SkinCount <= 0 Then
            SkinCount = 0
        End If
        Dim obs(2) As Object
        For index As Integer = 0 To _dt.Rows.Count - 1
            If index >= SkinCount And index < SkinCount + PageSize Then
                obs(0) = _dt.Rows(index)(ColName1).ToString()
                obs(1) = _dt.Rows(index)(ColName2).ToString()
                Me.MyDataView1.Rows.Add(obs)
            End If
        Next

        '设置下面的按钮
        RefreshButton()
        '设置当前选的位置
        SetSelectPos(PagePos)
        '
        If MyDataView1.Rows.Count = 0 Then
            RaiseEvent ItemClickRefreshData("", bState)
        End If
        If PageIndex <= 0 Then
            PageIndex = 1
        ElseIf PageIndex >= PageCount Then
            PageIndex = PageCount
        End If
        Me.txtPage.Text = PageIndex.ToString()

        RaiseEvent EventPageComplete() 'A-2010-02-14
    End Sub
#End Region
End Class
